using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace TripTracker
{
    class Settings
    {
        private String gpsPort = "";
        public String GPSPort {
            get {
                if (gpsPort != null)
                    return gpsPort;
                else
                    throw new NullReferenceException();
            }
            set {
                gpsPort = value;
            }
        }

        private bool webTrackerActivated = false;
        public bool WebTrackerActivated
        {
            get
            {
                return webTrackerActivated;
            }
            set
            {
                webTrackerActivated = value;
            }
        }

        private String webTrackerUserName = "";
        public String WebTrackerUserName
        {
            get
            {
                return webTrackerUserName;
            }
            set
            {
                webTrackerUserName = value;
            }
        }

        private String webTrackerPassword = "";
        public String WebTrackerPassword
        {
            get
            {
                return webTrackerPassword;
            }
            set
            {
                webTrackerPassword = value;
            }
        }

        private int webTrackerSeconds = 10;
        public int WebTrackerSeconds
        {
            get
            {
                return webTrackerSeconds;
            }
            set
            {
                webTrackerSeconds = value;
            }
        }

        private static Settings settings;

        private Settings() {
            loadSettings();
        }

        public static Settings getSettings() {
            if (settings == null)
                settings = new Settings();
            
            return settings;
        }

        /// <summary>
        /// Loads the settings of the application, if stored
        /// </summary>
        private void loadSettings()
        {
            //read file and load settings
            try
            {
                StreamReader sr = File.OpenText("TripTrackerSettings.ini");
                string input = null;

                while ((input = sr.ReadLine()) != null)
                {
                    string[] currentLine = input.Split('=');

                    switch (currentLine[0])
                    {
                        case "GPSPort":
                            GPSPort = currentLine[1];
                            break;
                        case "Activated":
                            try
                            {
                                webTrackerActivated = System.Convert.ToBoolean(currentLine[1]);
                            }
                            catch (System.FormatException e)
                            {
                                MessageBox.Show("Warning! " + e.Message);
                            }
                            break;
                        case "Username":
                            webTrackerUserName = currentLine[1];
                            break;
                        case "Password":
                            webTrackerPassword = currentLine[1];
                            break;
                        case "Seconds":
                            try
                            {
                                webTrackerSeconds = System.Convert.ToInt32(currentLine[1]);
                            }
                            catch (System.FormatException e)
                            {
                                MessageBox.Show("Warning! " + e.Message);
                            } 
                            break;
                    }

                }
                sr.Close();
            }
            catch (FileNotFoundException)
            {

            }
            
        }

        /// <summary>
        /// Receives the settungs specified in the application for saving them on the settings-file
        /// </summary>
        /// <param name="GPSPort">COM port</param>
        /// <param name="WebTrackerActivated">webtracking activated</param>
        /// <param name="WebTrackerUserName">webtracking username</param>
        /// <param name="WebTrackerPassword">webtracking password</param>
        /// <param name="WebTrackerSeconds">webtracking seconds to wait</param>
        public void setSettings(String GPSPort, bool WebTrackerActivated, String WebTrackerUserName, String WebTrackerPassword, int WebTrackerSeconds)
        {
            this.GPSPort = GPSPort;
            this.WebTrackerActivated = WebTrackerActivated;
            this.WebTrackerUserName = WebTrackerUserName;
            this.WebTrackerPassword = WebTrackerPassword;
            this.WebTrackerSeconds = WebTrackerSeconds;

            writeSettings();
        }
        
        /// <summary>
        /// Writes the settings-file
        /// </summary>
        private void writeSettings()
        {
            //write settings on file

            FileInfo f = new FileInfo("TripTrackerSettings.ini");
            StreamWriter sw = f.CreateText();
            sw.WriteLine("[General]");
            sw.WriteLine("");
            sw.WriteLine("[GPS]");
            sw.WriteLine("GPSPort=" + this.GPSPort);
            sw.WriteLine("");
            sw.WriteLine("[WEBTracker]");
            sw.WriteLine("Username="+ this.WebTrackerUserName);
            sw.WriteLine("Password="+ this.WebTrackerPassword);
            sw.WriteLine("Activated=" + this.WebTrackerActivated.ToString());
            sw.WriteLine("Seconds=" + this.WebTrackerSeconds);
            sw.WriteLine("");
            sw.Close();
        }
    }
}
